package com.bonana.backend.controller;

import com.alibaba.fastjson.JSON;
import com.bonana.core.entity.PartnerUser;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.core.services.PartnerService;
import com.bonana.utils.TimestampUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * Created by xiabin on 16/5/21.
 */
@Controller
@RequestMapping(value="/partner")
public class PartnerManagerController {
    @Autowired
    PartnerService partnerService;

    @RequestMapping(value="/addPartner")
    public String addPartner() {
        return "partner/addPartner";
    }

    @RequestMapping(value="/SavePartner")
    @ResponseBody
    public Response SavePartner(@ModelAttribute("PartnerUser")PartnerUser user) {
        if (user.getUid() == 0) {
            user.setCreateTime(TimestampUtil.now());
            user.setUpdateTime(TimestampUtil.now());
            if (user.getPassword() == null) {
                user.setPassword(user.getUsername());
            }
            user.setPassword(DigestUtils.md5Hex(user.getUsername() + user.getPassword()));
            user.setStatus((byte) 0);
            partnerService.AddPartner(user);
        } else {
            user.setUpdateTime(TimestampUtil.now());
            if (user.getPassword() != null) {
                user.setPassword(DigestUtils.md5Hex(user.getUsername() + user.getPassword()));
            }
            partnerService.UpdatePartner(user);
        }
        return new Response(ResponseCode.SUCC);
    }

    @RequestMapping(value="getPartner", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo getPartner(Integer pgNum, Integer pgSize, @RequestParam(required = false) String query) {
        List<PartnerUser> list = partnerService.getPartner(pgNum, pgSize, query);
        return new PageInfo(list);
    }

    @RequestMapping(value="queryPartner", method = RequestMethod.POST)
    @ResponseBody
    public PartnerUser queryPartner(Integer uid) {
        return partnerService.getPartnerById(uid);
    }

    @RequestMapping(value="/manager")
    public String init() {
        return "partner/manager";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Response delBanner(int parterId) {
        int ret = partnerService.DelPartnerById(parterId);
        if (ret == 1) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ITEM_NOT_FOUND);
        }
    }

    @RequestMapping("/disable")
    @ResponseBody
    public Response disable(int parterId) {
        PartnerUser us = partnerService.getPartnerById(parterId);
        if (us != null) {
            byte old = us.getStatus();
            if (old == 0) {
                us.setStatus((byte) 1);
            } else {
                us.setStatus((byte) 0);
            }
            us.setUpdateTime(TimestampUtil.now());
            partnerService.UpdatePartner(us);
            return new Response(ResponseCode.SUCC);
        }
        return new Response(ResponseCode.ERROR);
    }
}
